package mblog.web.controller.admin;

import mblog.core.api.WebRestResponse;
import mblog.core.data.*;
import mblog.core.persist.entity.WzclCgJsjdPO;
import mblog.core.persist.service.AuthMenuService;
import mblog.core.persist.service.MaterialService;
import mblog.core.persist.service.RoleService;
import mblog.core.persist.service.WzclCgJsjdService;
import mblog.web.controller.BaseController;
import mtons.modules.pojos.Data;
import mtons.modules.pojos.Paging;
import mtons.modules.pojos.UserProfile;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zhangyue on 16/4/7.
 */

@Controller("p_project_ctl")
@RequestMapping("/admin/project")
public class ProjectController extends BaseController{


    @Autowired
    private MaterialService materialService;

    @Autowired
    private WzclCgJsjdService wzclCgJsjdService;

    @RequestMapping("/list")
    public String list(Integer pn,String key,ModelMap model) {
        Paging page = wrapPage(pn);
        wzclCgJsjdService.paging(page, key);
        model.put("page", page);
        model.put("key", key);

        return "/admin/project/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        materialService.delete(id);
        return "redirect:/admin/project/list";
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam(value = "id",required = false,defaultValue = "0") long id, Model model) {
        if (id != 0) {
            PMaterial material = materialService.findById(id);
            model.addAttribute("material", material);
        }
        return "/admin/project/edit";
    }

    /**
     *
     * @param pj 属性类型尽量都是以字符串,严格一致
     * @param model
     * @return
     */
    @RequestMapping("/save")
    public String save(@ModelAttribute(value = "pj") WzclCgJsjd pj, @RequestParam(value = "file", required = false) CommonsMultipartFile execl,ModelMap model) {
        try {

            //上传一个execl文件到服务器指定目录
//            String dest = "/Users/fotoplace/zydev/data/execl-files/";
//            InputStream ins =  execl.getInputStream();
//            File file = new File(dest+System.currentTimeMillis()+".xls");
//                OutputStream os = new FileOutputStream(file);
//                int bytesRead = 0;
//                byte[] buffer = new byte[8192];
//                while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
//                    os.write(buffer, 0, bytesRead);
//                }
//                os.close();
//                ins.close();

          //  wzclCgJsjdService.save(pj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/admin/project/list";
    }

}
